/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgameslogic;

import java.io.Serializable;

/**
 *
 * @author 75
 */
public class EditionEventTeamParticipation extends EditionEventParticipation implements Serializable, Comparable<Object> {

   private Team team;
   private Result result;

   public EditionEventTeamParticipation() {
   }

   public EditionEventTeamParticipation(EditionDisciplineEvent editionDisciplineEvent, Edition edition) {
      super(editionDisciplineEvent, edition);
   }

   public EditionEventTeamParticipation(Team team, Result result) {
      this.team = team;
      this.result = result;
   }

   public EditionEventTeamParticipation(Team team, Result result, EditionDisciplineEvent editionDisciplineEvent, Edition edition) {
      super(editionDisciplineEvent, edition);
      this.team = team;
      this.result = result;
   }

   /**
    * @return the team
    */
   public Team getTeam() {
      return team;
   }

   /**
    * @return the result
    */
   public Result getResult() {
      return result;
   }

   /**
    * @param team the team to set
    */
   public void setTeam(Team team) {
      this.team = team;
   }

   /**
    * @param result the result to set
    */
   public void setResult(Result result) {
      this.result = result;
   }

   @Override
   public String toString() {
      return getTeam() + " - Resultado: " + getResult();
   }

   @Override
   public int compareTo(Object o) {
      EditionEventTeamParticipation edEvPart = (EditionEventTeamParticipation) o;
      ClassificationType ct = this.getEditionDisciplineEvent().getEditionDisciplineEvent().getEventDiscipline().getcType();


      if (ct.getOrder().equals(ClassificationType.ORDER.H)) {

	 if (result instanceof ResultTime) {
	    if (!((ResultTime) result).getTime().maior(((ResultTime) (edEvPart.getResult())).getTime())) {
	       return 1;
	    } else if (((ResultTime) result).getTime().maior(((ResultTime) (edEvPart.getResult())).getTime())) {
	       return -1;
	    } else {
	       return 0;
	    }


	 } else if (result instanceof ResultPoints) {
	    if (((ResultPoints) result).getPoints() < ((ResultPoints) (edEvPart.getResult())).getPoints()) {
	       return 1;
	    } else if (((ResultPoints) result).getPoints() > ((ResultPoints) (edEvPart.getResult())).getPoints()) {
	       return -1;
	    } else {
	       return 0;
	    }

	 } else if (result instanceof ResultRank) {
	    if (((ResultRank) result).getRank() < ((ResultRank) (edEvPart.getResult())).getRank()) {
	       return 1;
	    } else if (((ResultRank) result).getRank() > ((ResultRank) (edEvPart.getResult())).getRank()) {
	       return -1;
	    } else {
	       return 0;
	    }
	 } else if (result instanceof ResultDistance) {
	    if (((ResultDistance) result).getDistance() < ((ResultDistance) (edEvPart.getResult())).getDistance()) {
	       return 1;
	    } else if (((ResultDistance) result).getDistance() > ((ResultDistance) (edEvPart.getResult())).getDistance()) {
	       return -1;
	    } else {
	       return 0;
	    }
	 }

      } else if (ct.getOrder().equals(ClassificationType.ORDER.L)) {

	 if (result instanceof ResultTime) {
	    if (((ResultTime) result).getTime().maior(((ResultTime) (edEvPart.getResult())).getTime())) {
	       return 1;
	    } else if (!((ResultTime) result).getTime().maior(((ResultTime) (edEvPart.getResult())).getTime())) {
	       return -1;
	    } else {
	       return 0;
	    }



	 } else if (result instanceof ResultPoints) {
	    if (((ResultPoints) result).getPoints() > ((ResultPoints) (edEvPart.getResult())).getPoints()) {
	       return 1;
	    } else if (((ResultPoints) result).getPoints() < ((ResultPoints) (edEvPart.getResult())).getPoints()) {
	       return -1;
	    } else {
	       return 0;
	    }

	 } else if (result instanceof ResultRank) {
	    if (((ResultRank) result).getRank() > ((ResultRank) (edEvPart.getResult())).getRank()) {
	       return 1;
	    } else if (((ResultRank) result).getRank() < ((ResultRank) (edEvPart.getResult())).getRank()) {
	       return -1;
	    } else {
	       return 0;
	    }
	 } else if (result instanceof ResultDistance) {
	    if (((ResultDistance) result).getDistance() > ((ResultDistance) (edEvPart.getResult())).getDistance()) {
	       return 1;
	    } else if (((ResultDistance) result).getDistance() < ((ResultDistance) (edEvPart.getResult())).getDistance()) {
	       return -1;
	    } else {
	       return 0;
	    }
	 }

      }

      return 0;

   }

   @Override
   public void assignMedal(Medal.MEDALTYPE medalType) {

      
      EditionDisciplineEvent edDiscEv = this.getEditionDisciplineEvent();
      
      MedalTeam m = new MedalTeam(team,  edDiscEv,  medalType);
	//Tenho que atribuir a medalha ao pais aqui para não correr o risco de duplicar as medalhas 
      
      for (Athlete a: team.getAthletes()) {
	a.addMedal(m); 
      }
      team.getDelegation().addMedal(m);
      team.getDelegation().getCountry().addMedal(m);
      
      
      
      
      
      



   }

   @Override
   public Nation getNationParticipation() {
      return  team.getDelegation().getCountry();
   }
}
